#include "esp_log.h"
#include "board.h"
#include "esp_peripherals.h"
#include "periph_adc_button.h"
#include "input_key_service.h"

#include "app_key_service.h"
#include "app_music_play.h"

static const char *TAG = "app.key.service";

extern esp_periph_set_handle_t set;

static esp_err_t music_vol_adjust(uint8_t direction, uint8_t value)
{
    int vol = 0;
    app_audio_get_vol(&vol);
    if(direction){
        if(vol < 70)
            vol = vol + value;
    }else{
        if(vol > 20)
            vol = vol - value;
    }

    app_audio_set_vol(vol);

    ESP_LOGI(TAG, "vol set to %d", vol);

    return ESP_OK;
}

static esp_err_t input_key_service_cb(periph_service_handle_t handle, periph_service_event_t *evt, void *ctx)
{
    ESP_LOGD(TAG, "[ * ] input key id is %d, %d", (int)evt->data, evt->type);
    const char *key_types[INPUT_KEY_SERVICE_ACTION_PRESS_RELEASE + 1] = {"UNKNOWN", "CLICKED", "CLICK RELEASED", "PRESSED", "PRESS RELEASED"};
    switch ((int)evt->data) {
        case INPUT_KEY_USER_ID_REC:
            ESP_LOGI(TAG, "[ * ] [Rec] KEY %s", key_types[evt->type]);
            if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            {
                if(app_audio_is_runing())
                {
                    app_audio_stop();
                }else
                {
                    app_audio_stop();
                    app_audio_play(NULL, 0);
                }
            }

            break;
        case INPUT_KEY_USER_ID_SET:
            ESP_LOGI(TAG, "[ * ] [SET] KEY %s", key_types[evt->type]);
            if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            {
                // if(app_audio_is_runing())
                // {
                //     app_audio_stop();
                //     app_audio_playlist_sd_next(1);
                // }else
                // {
                //     app_audio_stop();
                //     app_audio_play(NULL, 0);
                // }
            }
            break;
        case INPUT_KEY_USER_ID_PLAY:
            ESP_LOGI(TAG, "[ * ] [Play] KEY %s", key_types[evt->type]);
            // if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            // {
            //     if(app_audio_is_runing())
            //     {
            //         app_audio_stop();
            //     }else
            //     {
            //         app_audio_stop();
            //         app_audio_play(NULL, 0);
            //     }
            // }
            break;
        case INPUT_KEY_USER_ID_MODE:
            ESP_LOGI(TAG, "[ * ] [MODE] KEY %s", key_types[evt->type]);
            if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            {
                if(app_audio_is_runing())
                {
                    app_audio_stop();
                    app_audio_playlist_sd_next(1);
                }else
                {
                    app_audio_stop();
                    app_audio_play(NULL, 0);
                }
            }
            break;
        case INPUT_KEY_USER_ID_VOLDOWN:
            ESP_LOGI(TAG, "[ * ] [Vol-] KEY %s", key_types[evt->type]);
            if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            {
                music_vol_adjust(0, 5);
            }
            break;
        case INPUT_KEY_USER_ID_VOLUP:
            ESP_LOGI(TAG, "[ * ] [Vol+] KEY %s", key_types[evt->type]);
            if(evt->type == INPUT_KEY_SERVICE_ACTION_CLICK)
            {
                music_vol_adjust(1, 5);
            }
            break;
        case INPUT_KEY_USER_ID_MUTE:
            ESP_LOGI(TAG, "[ * ] [MUTE] KEY %s", key_types[evt->type]);
            break;
        case INPUT_KEY_USER_ID_CAPTURE:
            ESP_LOGI(TAG, "[ * ] [CAPTURE] KEY %s", key_types[evt->type]);
            break;
        case INPUT_KEY_USER_ID_MSG:
            ESP_LOGI(TAG, "[ * ] [MSG] KEY %s", key_types[evt->type]);
            break;
        case INPUT_KEY_USER_ID_BATTERY_CHARGING:
            ESP_LOGI(TAG, "[ * ] [BATTERY_CHARGING] KEY %s", key_types[evt->type]);
            break;
        case INPUT_KEY_USER_ID_WAKEUP:
            ESP_LOGI(TAG, "[ * ] [WAKEUP] KEY %s", key_types[evt->type]);
            break;
        case INPUT_KEY_USER_ID_COLOR:
            ESP_LOGI(TAG, "[ * ] [COLOR] KEY %s", key_types[evt->type]);
            break;
        default:
            ESP_LOGE(TAG, "User Key ID[%d] does not support", (int)evt->data);
            break;
    }

    return ESP_OK;
}

void initialise_key(void)
{
    ESP_LOGI(TAG, "Initialize Button peripheral with board init");
    audio_board_key_init(set);

    input_key_service_info_t input_key_info[] = INPUT_KEY_DEFAULT_INFO();
    input_key_service_cfg_t input_cfg = INPUT_KEY_SERVICE_DEFAULT_CONFIG();
    input_cfg.handle = set;
    input_cfg.based_cfg.task_stack = 4 * 1024;
    periph_service_handle_t input_ser = input_key_service_create(&input_cfg);

    input_key_service_add_key(input_ser, input_key_info, INPUT_KEY_NUM);
    periph_service_set_callback(input_ser, input_key_service_cb, NULL);
}




